package com.ruoyi.project.wxuser.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.wxuser.domain.UserUpdate;
import com.ruoyi.project.wxuser.service.IUserUpdateService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 更新审核Controller
 * 
 * @author wyh
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/wxuser/updateUser")
public class UserUpdateController extends BaseController
{
    @Autowired
    private IUserUpdateService userUpdateService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询更新审核列表
     */
    @PreAuthorize("@ss.hasPermi('wxuser:updateUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserUpdate userUpdate)
    {
        //非管理部门限制用户只能查看本部门提交的申请
        Long deptId = SecurityUtils.getDeptId();
        if(deptId != 103L){
            userUpdate.setTopInviterId(SecurityUtils.getUserId());
        }

        startPage();
        List<UserUpdate> list = userUpdateService.selectUserUpdateList(userUpdate);
        return getDataTable(list);
    }

    /**
     * 导出更新审核列表
     */
    @PreAuthorize("@ss.hasPermi('wxuser:updateUser:export')")
    @Log(title = "更新审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserUpdate userUpdate)
    {
        List<UserUpdate> list = userUpdateService.selectUserUpdateList(userUpdate);
        ExcelUtil<UserUpdate> util = new ExcelUtil<UserUpdate>(UserUpdate.class);
        util.exportExcel(response, list, "更新审核数据");
    }

    /**
     * 获取更新审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('wxuser:updateUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userUpdateService.selectUserUpdateById(id));
    }

    /**
     * 新增更新审核
     */
    @PreAuthorize("@ss.hasPermi('wxuser:updateUser:add')")
    @Log(title = "更新审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserUpdate userUpdate)
    {
        return toAjax(userUpdateService.insertUserUpdate(userUpdate));
    }

    /**
     * 修改更新审核
     */
    @PreAuthorize("@ss.hasPermi('wxuser:updateUser:edit')")
    @Log(title = "更新审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserUpdate userUpdate)
    {
        //审核状态
        String status = userUpdate.getStatus();
        if(status.equals("已通过")){
            //更新用户信息
            SysUser sysUser = new SysUser();
            sysUser.setNickName(userUpdate.getNickName());
            sysUser.setBrith(userUpdate.getBrith());
            sysUser.setAvatar(userUpdate.getAvatar());
            sysUser.setHeight(userUpdate.getHeight());
            sysUser.setWeight(userUpdate.getWeight());
            sysUser.setOccupation(userUpdate.getOccupation());
            sysUser.setCity(userUpdate.getCity());
            sysUser.setCityCode(userUpdate.getCityCode());
            sysUser.setUserId(userUpdate.getUserId());

            userService.updateUser(sysUser);

            //更新记录状态
            userUpdateService.updateUserUpdate(userUpdate);
        }

        return AjaxResult.success("操作成功");
    }

    /**
     * 删除更新审核
     */
    @PreAuthorize("@ss.hasPermi('wxuser:updateUser:remove')")
    @Log(title = "更新审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userUpdateService.deleteUserUpdateByIds(ids));
    }
}
